from rest_framework import serializers

from apps.goods.models import SPU, Brand, GoodsCategory


class SPUGoodsSerializer(serializers.ModelSerializer):
    """
    SPU序列化器
    """
    # 品牌
    brand = serializers.StringRelatedField(read_only=True)
    brand_id = serializers.IntegerField()

    # 分类
    # 在保存时报错,前端报400错,以下三个字段必须验证，所以加上read_only = True
    category1 = serializers.StringRelatedField(read_only=True)
    category2 = serializers.StringRelatedField(read_only=True)
    category3 = serializers.StringRelatedField(read_only=True)

    category1_id = serializers.IntegerField()
    category2_id = serializers.IntegerField()
    category3_id = serializers.IntegerField()

    class Meta:
        model = SPU
        fields = "__all__"

class BrandSerailizer(serializers.ModelSerializer):
    """
    品牌序列化器
    """
    class Meta:
        model = Brand
        fields = "__all__"


class GoodsCategorySerailizer(serializers.ModelSerializer):
    """
    商品分类序列化器
    """
    class Meta:
        model = GoodsCategory
        fields = "__all__"